package com.zhou.framework.util;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.service.IService;
import com.zhou.util.ObjectUtil;
import org.apache.commons.collections.CollectionUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author lang.zhou
 * @since 2022/11/22 9:19
 */
public class MybatisPlusBatchUtil {

    /**
     * 批量删除通过id关联的表
     * @param mapper    mapper
     * @param func      关联字段
     * @param ids       父id集合
     */
    public static <T> int batchDelete(BaseMapper<T> mapper, SFunction<T, Object> func, Collection<?> ids){
        if(CollectionUtils.isNotEmpty(ids)){
            LambdaUpdateWrapper<T> lambda = new LambdaUpdateWrapper<T>();
            lambda.in(func,ids);
            return mapper.delete(lambda);
        }
        return 0;
    }

    public static <T> int delete(IService<T> service, SFunction<T, Object> func, Object id){
        return delete(service.getBaseMapper(),func,id);
    }
    public static <T> int delete(BaseMapper<T> mapper, SFunction<T, Object> func, Object id){
        if(ObjectUtil.isNotEmpty(id)){
            LambdaQueryWrapper<T> lambda = new QueryWrapper<T>().lambda();
            lambda.eq(func,id);
            return mapper.delete(lambda);
        }
        return 0;
    }

    public static <T> int batchDelete(IService<T> service, SFunction<T, Object> func, Collection<?> ids){
        if(CollectionUtils.isNotEmpty(ids)){
            BaseMapper<T> mapper = service.getBaseMapper();
            LambdaUpdateWrapper<T> lambda = new LambdaUpdateWrapper<>();
            lambda.in(func,ids);
            return mapper.delete(lambda);
        }
        return 0;
    }

    /**
     * 通过属性查询
     * @param mapper     mapper
     * @param func      属性字段
     * @param o         属性值
     */
    public static <T> List<T> batchList(BaseMapper<T> mapper, SFunction<T, Object> func, Object o){
        LambdaQueryWrapper<T> lambda = new LambdaQueryWrapper<>();
        lambda.eq(func,o);
        return mapper.selectList(lambda);
    }
    public static <T> List<T> batchList(IService<T> service, SFunction<T, Object> func, Object o){
        return batchList(service.getBaseMapper(),func,o);
    }

    /**
     * 通过属性批量in查询（一般查询通过id关联的子表数据）
     * @param mapper     mapper
     * @param func      属性字段
     * @param ids       属性值
     */
    public static <T> List<T> batchList(BaseMapper<T> mapper, SFunction<T, Object> func, Collection<?> ids){
        if(CollectionUtils.isNotEmpty(ids)){
            LambdaQueryWrapper<T> lambda = new LambdaQueryWrapper<>();
            lambda.in(func,ids);
            return mapper.selectList(lambda);
        }
        return new ArrayList<>(0);
    }

    public static <T> List<T> batchList(IService<T> service, SFunction<T, Object> func, Collection<?> ids){
        if(CollectionUtils.isNotEmpty(ids)){
            BaseMapper<T> baseMapper = service.getBaseMapper();
            return batchList(baseMapper,func,ids);
        }
        return new ArrayList<>(0);
    }


}
